package tree.test222;

import listbyorder.utils.TreeNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/17 11:19
 */
public class Solution1 {

    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        // 首先计算出左右子树的高度
        int l = getH(root.left);
        int r = getH(root.right);
        if (l == r) return ((1 << l) - 1) + countNodes(root.right) + 1;
        return ((1 << r) - 1) + countNodes(root.left) + 1;
    }

    private int getH(TreeNode root) {
        int ans = 0;
        while (root != null) {
            ans++;
            root = root.left;
        }
        return ans;
    }
}
